const Base = require('./base.js');

module.exports = class extends Base {
  async cascaderAction() {
    let status = [this.get('status')];

    if (this.get('status') === '9') {
      status = [2, 1];
    } else if (this.get('status') === '10') {
      status = [0, 1, 2];
    }

    const arr = [];
    const list = await this.model('project_list')
      // .where(query)
      .select();
    if (this.get('type') === '2') {
      for (const item of list) {
        if (item.type === 1) {
          item.children = [];
          for (const itemBuilding of list) {
            if (
              itemBuilding.type === 2 &&
              itemBuilding.project_id === item.id
            ) {
              // itemBuilding.children = [];
              // for (const itemFloor of list) {
              //   if (
              //     itemFloor.type === 3 &&
              //     itemFloor.building_id === itemBuilding.id
              //   ) {
              //     itemBuilding.children.push(itemFloor);
              //   }
              // }
              item.children.push(itemBuilding);
            }
          }
          arr.push(item);
        }
      }
      return this.success(arr);
    }

    if (this.get('type') === '3') {
      for (const item of list) {
        if (item.type === 1) {
          item.children = [];
          for (const itemBuilding of list) {
            if (
              itemBuilding.type === 2 &&
              itemBuilding.project_id === item.id
            ) {
              itemBuilding.children = [];
              for (const itemFloor of list) {
                if (
                  itemFloor.type === 3 &&
                  itemFloor.building_id === itemBuilding.id &&
                  status.includes(itemFloor.status)
                ) {
                  itemBuilding.children.push(itemFloor);
                }
              }
              item.children.push(itemBuilding);
            }
          }
          arr.push(item);
        }
      }
      return this.success(arr);
    }

    if (this.get('type') === '4') {
      for (const item of list) {
        if (item.type === 1) {
          item.children = [];
          for (const itemBuilding of list) {
            if (
              itemBuilding.type === 2 &&
              itemBuilding.project_id === item.id
            ) {
              itemBuilding.children = [];
              for (const itemFloor of list) {
                if (
                  itemFloor.type === 3 &&
                  itemFloor.building_id === itemBuilding.id
                ) {
                  itemFloor.children = [];
                  for (const itemRoom of list) {
                    if (
                      itemRoom.type === 4 &&
                      itemRoom.floor_id === itemFloor.id &&
                      status.includes(itemRoom.status)
                    ) {
                      itemFloor.children.push(itemRoom);
                    }
                  }
                  itemBuilding.children.push(itemFloor);
                }
              }
              item.children.push(itemBuilding);
            }
          }
          arr.push(item);
        }
      }
      return this.success(arr);
    }
  }

  async listProjectAction() {
    const data = await this.model('project_list')
      .where({ type: 1 })
      .select();
    for (const item of data) {
      item.left =
        (await this.model('project_list')
          .where({ type: 4, project_id: item.id, status: 1 })
          .count()) || 0;
    }
    return this.success(data);
  }

  async listbuildingAction() {
    const query = { type: 2 };
    if (!think.isEmpty(this.get('id'))) {
      query.project_id = this.get('id');
      const data = await this.model('project_list')
        .where(query)
        .order(['id DESC'])
        .select();
      return this.success(data);
    } else {
      const data = await this.model('project_list')
        .where(query)
        .order(['id DESC'])
        .select();
      return this.success(data);
    }
  }

  async listFloorAction() {
    const query = { type: 3 };
    if (!think.isEmpty(this.get('id'))) {
      query.building_id = this.get('id');
      const data = await this.model('project_list')
        .where(query)
        .order(['id DESC'])
        .select();
      return this.success(data);
    } else {
      const data = await this.model('project_list')
        .where(query)
        .order(['id DESC'])
        .select();
      return this.success(data);
    }
  }

  async listOriginAction() {
    const query = { type: 5 };
    if (!think.isEmpty(this.get('id'))) {
      query.floor_id = this.get('id');
      const data = await this.model('project_list')
        .where(query)
        .order(['id DESC'])
        .select();
      return this.success(data);
    } else {
      const data = await this.model('project_list')
        .where(query)
        .order(['id DESC'])
        .select();
      return this.success(data);
    }
  }

  async listRoomAction() {
    const status = this.get('status');
    const query = { type: 4 };
    if (!think.isEmpty(status)) {
      query.status = status;
    }
    if (!think.isEmpty(this.get('id'))) {
      query.floor_id = this.get('id');
      const data = await this.model('project_list')
        .where(query)
        .order(['id DESC'])
        .select();
      return this.success(data);
    } else {
      const data = await this.model('project_list')
        .where(query)
        .order(['id DESC'])
        .select();
      return this.success(data);
    }
    return this.success([]);
  }

  // async putAction() {
  //   const values = this.post();
  //   const id = this.post('id');
  //   const model = this.model('industry_list');

  //   if (id > 0) {
  //     values.update_time = ['exp', 'CURRENT_TIMESTAMP()'];
  //     await model.where({ id: id }).update(values);
  //   } else {
  //     delete values.id;
  //     values.create_time = ['exp', 'CURRENT_TIMESTAMP()'];
  //     await model.add(values);
  //   }
  //   return this.success(values);
  // }

  // async deleteAction() {
  //   const id = this.get('id');
  //   await this.model('industry_list')
  //     .where({ id: id })
  //     .limit(1)
  //     .delete();

  //   return this.success();
  // }

  async infoAction() {
    const arr = this.post('info');
    const data = await this.model('project_list').select();
    const project = [];
    for (const item of arr) {
      let p, b, r, f;
      data.forEach((res) => {
        if (res.id === item[0]) {
          p = res.name;
        }
        if (res.id === item[1]) {
          b = res.name;
        }
        if (res.id === item[2]) {
          f = res.name;
        }
        if (res.id === item[3]) {
          r = res.name;
        }
      });
      project.push({
        project: p,
        building: b,
        floor: f,
        room: r,
      });
    }

    return this.success(project);
  }
};
